﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chinaer.Core.Helper
{
    public static class Extensions
    {
        /// <summary>
        /// 获取Exception返回的jsonResult值
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static Model.Common.JsonResult ExceptionJsonResult(this Exception ex)
        {
            Model.Common.JsonResult jsonResult = new Model.Common.JsonResult();
            jsonResult.Success = false;
            jsonResult.ErrorMessage = Chinaer.Core.Helper.ExceptionMessageHelper.RollException(ex);
            jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.Exception;
            return jsonResult;
        }
        /// <summary>
        /// 判断实体是否为NUll 如果为Null 则返回对应的JsonResult 否则返回null
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static Chinaer.Model.Common.JsonResult CheckIsNull(this object obj)
        {
            Model.Common.JsonResult jsonResult = new Model.Common.JsonResult();
            if (obj == null)
            {
                jsonResult.Success = false;
                jsonResult.ErrorMessage = "实体为Null";
                jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.EntityIsNull;
            }
            else if (obj.GetType() == typeof(string))
            {
                if (obj.ToString().Trim().Length == 0)
                {
                    jsonResult.Success = false;
                    jsonResult.ErrorMessage = "实体为Null";
                    jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.EntityIsNull;
                }
                else
                {
                    jsonResult.Success = true;
                }
            }
            else
            {
                jsonResult.Success = true;
            }
            return jsonResult;
        }
        /// <summary>
        /// 返回结果为Extists的jsonResult结果
        /// </summary>
        /// <param name="jsonResult"></param>
        /// <returns></returns>
        public static Chinaer.Model.Common.JsonResult JsonResultExists(this Model.Common.JsonResult jsonResult,string errorMessage=null)
        {
            Model.Common.JsonResult jsonResult1 = new Model.Common.JsonResult();
            jsonResult1.Success = false;
            if (errorMessage != null)
            {
                jsonResult1.ErrorMessage = errorMessage;
            }
            else
            {
                jsonResult1.ErrorMessage = "已经存在";
            }
            jsonResult1.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.IsExists;
            return jsonResult1;
        }

        /// <summary>
        /// 返回结果为Extists的jsonResult结果
        /// </summary>
        /// <param name="jsonResult"></param>
        /// <returns></returns>
        public static Chinaer.Model.Common.JsonResult JsonResultNull(this Model.Common.JsonResult jsonResult,string errorMessage=null)
        {
            Model.Common.JsonResult jsonResult1 = new Model.Common.JsonResult();
            jsonResult1.Success = false;
            if (string.IsNullOrEmpty(errorMessage))
            {
                jsonResult1.ErrorMessage = "为Null";
            }
            else
            {
                jsonResult1.ErrorMessage = errorMessage;
            }            
            jsonResult1.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.IsExists;
            return jsonResult1;
        }
    }
}
